package com.igeekhome.controller.api;

import com.igeekhome.pojo.DNetwork;
import com.igeekhome.pojo.SysStaff;
import com.igeekhome.service.ILoginService;
import com.igeekhome.service.IStaffService;
import com.igeekhome.service.ISysPermissionService;
import com.igeekhome.util.NavBean;
import com.igeekhome.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 立以宁
 */
@RestController
@RequestMapping("/api/staff")
public class ApiController {


    @Autowired
    private ISysPermissionService sysPermissionService;

    @Autowired
    private IStaffService staffService;

    @Autowired
    private ILoginService loginService;

    /**
     * 修改用户名和密码操作
     * @return
     */
    @RequestMapping("/update")
    public ResultBean update(String loginPassword, String name,HttpSession session) {
        SysStaff sysStaff = new SysStaff();
        sysStaff.setName(name);
        sysStaff.setLoginPassword(loginPassword);
        return staffService.updateStaff(sysStaff);
    }

    /**
     * 注销操作
     * @param session
     * @return
     */
    @RequestMapping("/loginout")
    public ResultBean loginOut(HttpSession session) {
        session.removeAttribute("staff");
        session.removeAttribute("network");
        return ResultBean.success();
    }

    @RequestMapping("/loginCheck")
    public ResultBean checkUser(String username, String password, HttpSession session, HttpServletResponse response) {
        if (username == null || password == null) {
            return ResultBean.fail("用户名密码不能为空");
        }
        Integer account = Integer.parseInt(username);
        SysStaff staff = staffService.login(account, password);

        ResultBean result = null;

        if (staff == null) {

            result = ResultBean.fail("用户名或密码不正确,请重新输入");
            return result;

        } else {
            DNetwork network = loginService.getNetwork(staff.getAffiliatedNetworkId());
            session.setAttribute("network", network);
            // 为了防止cookie外泄密码的密文，将登录密码置为null
            staff.setLoginPassword(null);
            session.setAttribute("staff", staff);
            // sessionid持久化(替换默认cookie有效期 -1)
            Cookie cookie = new Cookie("JSESSIONID", session.getId());

            cookie.setMaxAge(30 * 60);
            // 有效期30min

            response.addCookie(cookie);

            result = ResultBean.ok();
            return result;
        }

    }


    /**
     * 发送登录用户权限下的菜单给前台显示
     *
     * @param request
     * @return
     */
    @RequestMapping("/nav")
    public List<NavBean> getNavs(HttpServletRequest request) {
        //有问题，和前台不一定保持一致，这个user哪来的，session里
        SysStaff staff = (SysStaff) request.getSession().getAttribute("staff");
        List<NavBean> navs = sysPermissionService.listNavs(staff.getStaffId());
        return navs;
    }


}
